<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="java.util.*, java.io.*"%>
<%@page import="java.net.*"%>
<%
class Request{
 	public  String get(String url, HashMap<String, String> cookie) throws IOException{
	    String result = "", line;
		// 拼凑get请求的URL字串，使用URLEncoder.encode对特殊和不可见字符进行编码
		// String getURL = GET_URL + "&activatecode=" + URLEncoder.encode(url);
	    URL getUrl = new URL(url);
	    HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection();
	    connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");
	    connection.setRequestProperty("Host", getUrl.getHost());

	    if(cookie != null){ // 设置请求的 Cookie 写死了
	    	String cookieStr = "";
		    for (String key : cookie.keySet())cookieStr += key + "=" + cookie.get(key) + ";";
			
			cookieStr = cookieStr.substring(0, cookieStr.length()-1);
	    	connection.setRequestProperty("Cookie", cookieStr);
		}

	    connection.connect();

	    BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));

	    while ((line = reader.readLine()) != null){
	    	result += line;
	    	// lines = new String(line.getBytes(), "utf-8");
	        // System.out.println(line);
	    }

	    reader.close();
	    connection.disconnect();// 断开连接

	    return result;		
	}
 	public  String get(String url) throws IOException{
 		return get(url, null);
 	}
}

//json2hash foo = new json2hash(request);
%>
<%@page import="javax.script.*, sun.org.mozilla.javascript.internal.*" %>
<%
class JS_ScriptEngine extends javax.script.ScriptEngineManager{
	private javax.script.ScriptEngine js_engine = 
		   (javax.script.ScriptEngine) this.getEngineByExtension("js"); 
	
	public Object eval(String jsSource){
		Object result = null;
		
		try{
			result = js_engine.eval(jsSource);
		}catch(javax.script.ScriptException e){
			System.out.println("输入 js code 解析错误！ jsSource:" + jsSource);
			e.printStackTrace();
		}
		
		return result;
	}
	// JS 对象 转换到 Java Hash 对象
	public /* static */ Map<String, Object> NativeObject2Hash(NativeObject obj){
	    Map<String, Object> _data = new HashMap<String, Object>();
	    if(obj == null)return null;
	    
	    // 遍历对象
	    Object[] ids = obj.getAllIds();
	    for (Object id : ids) {
	        String newId = (String) id;
	        _data.put(newId, obj.get(newId, obj));
	        // System.out.println("\n" + newId);
	    }
	    return _data;
	}
	public /* static */ Map<String, Object>[] getArr(NativeArray nativeArr)throws IOException{
	    if(nativeArr == null)return new HashMap[0];
	    
	    Long len = nativeArr.getLength();
	    
	    Map<String, Object>[] arr  = new HashMap[len.intValue()];
	    
	    for (Object o : nativeArr.getIds()) {
	        Integer index = (Integer) o;
	        NativeObject nativeObj = (NativeObject)nativeArr.get(index, nativeArr);
	        HashMap<String, Object> item = (HashMap<String, Object>) NativeObject2Hash(nativeObj);
	        
	        arr[index.intValue()] = item;
	    }
	
	    return arr;
	}
}
%>

<%!
	public static HttpServletRequest _request = null;
	
	public static boolean isPhone(){
		boolean isPhone = false;
		
		if(_request == null)throw new NullPointerException("没有 请求 对象"); 
		
		String ua = _request.getHeader("User-Agent");
		
		if (ua == null || "".equals(ua))ua = "";

		if(ua.indexOf("iPhone") != -1 || ua.indexOf("iPad") != -1){
			isPhone = true;
		}
		
		return isPhone;
	
	}
%>
<%
	_request = request;
%>
